package com.c.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.c.entity.Information;
import com.c.service.InformationService;
import com.c.service.UserService;
import com.c.utils.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author wangxiangli
 * @create 2022-12-26-15:23
 */
@RestController
@RequestMapping("/information")
public class InformationController {

    @Autowired
    private InformationService informationService;
    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return informationService.pageQuery(queryPageBean);
    }

    /**
     * 文件上传
     *
     * @param imgFile
     * @return
     */
    @RequestMapping("/uploadImg")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {

        //将文件上传到阿里云服务器
        String fileName = AliyunUtils.upload3Aliyun(imgFile);
        if(fileName!=null){
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
        }
        return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Information information) {
        try {
            informationService.addInformation(information);
            return new Result(true, MessageConstant.COMMON_ADD_SUCCESS);//新增成功
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.COMMON_ADD_FAILE);//新增失败
        }
    }


    /**
     * 根据ID查询资讯信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/findInformationById")
    public Result findInformationById(Integer id) {
        try {
            Information information = informationService.getById(id);
            return new Result(true, MessageConstant.QUERY_COMMONSUCCESS, information);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_COMMONFAILE);
        }
    }

    /**
     * 编辑资讯信息
     *
     * @param information
     * @return
     */
    @RequestMapping("/editInformation")
    public Result editInformation(@RequestBody Information information) {
        try {
            informationService.editInformation(information);
            return new Result(true, MessageConstant.COMMON_EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.COMMON_EDIT_FAILE);
        }
    }

    /**
     * 删除资讯信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            informationService.removeById(id);
            return new Result(true, MessageConstant.COMMON_DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.COMMON_DELETE_FAILE);
        }
    }
}
